package com.zqq.goods.presenter

import com.zqq.baselibrary.ext.excute
import com.zqq.baselibrary.http.rx.BaseSubscriber
import com.zqq.baselibrary.presenter.BasePresenter
import com.zqq.goods.presenter.view.GoodsListView
import com.zqq.goods.service.GoodsService
import com.zqq.goods.service.bean.Goods
import com.zqq.goods.service.bean.GoodsSku
import javax.inject.Inject

/**
 * 商品列表 Presenter
 * wc
 * @author zqq on 2019/5/28.
 */
class GoodsListPresenter @Inject constructor() : BasePresenter<GoodsListView>() {

    @Inject
    lateinit var goodsService: GoodsService

    /**
     * 获取商品列表
     */
    fun getGoodsList(categoryId: Int, pageNo: Int) {
        if (!checkNetWork()) {
            return
        }
//        mView.showLoading()
        goodsService.getGoodsList(categoryId, pageNo)
                .excute(object : BaseSubscriber<MutableList<Goods>?>(mView) {
                    override fun onNext(t: MutableList<Goods>?) {
                        super.onNext(t)
                        mView.onGetGoodsListResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        var t: MutableList<Goods>? = getList()
                        mView.onGetGoodsListResult(t)
                    }
                }, lifecycleProvider)
    }

    private fun getList(): MutableList<Goods>? {
        var lists = mutableListOf<Goods>()
        var icon = "https://graph.baidu.com/resource/101f10c9ec8512b9f724901559113840.jpg"
        var banner = "http://pic32.nipic.com/20130902/13395269_103525238102_2.jpg"

        var sku1 = "红色"
        var sku2 = "蓝色"
        var sku3 = "路色"

        var skus = mutableListOf<GoodsSku>()
        skus.add(GoodsSku(22, "SKU标题", arrayListOf(sku1, sku2, sku3)))

        for (i in 1..10) {
            val good = Goods(1, 1, "商品描述", icon, 12345,
                    "细节1介绍", "细节2较少静安寺", i,
                    20, "code-goods", "默认SKU",
                    banner, skus, 2)

            lists.add(good)
        }
        return lists
    }

    /**
     * 根据关键字 搜索商品
     */
    fun getGoodsListByKeyword(keyword: String, pageNo: Int) {
        if (!checkNetWork()) {
            return
        }
//        mView.showLoading()
        goodsService.getGoodsListByKeyword(keyword, pageNo)
                .excute(object : BaseSubscriber<MutableList<Goods>?>(mView) {
                    override fun onNext(t: MutableList<Goods>?) {
                        super.onNext(t)
                        mView.onGetGoodsListResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        var t: MutableList<Goods>? = getListKey()
                        mView.onGetGoodsListResult(t)
                    }

                }, lifecycleProvider)
    }

    private fun getListKey(): MutableList<Goods>? {
        var lists = mutableListOf<Goods>()
        var icon = "https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=3bc7aa97e21190ef01fb95d9f620fa2b/4bed2e738bd4b31c39f3eec182d6277f9f2ff8f9.jpg"
        var banner = "http://pic32.nipic.com/20130902/13395269_103525238102_2.jpg"

        var sku1 = "https://imgsa.baidu.com/baike/pic/item/18d8bc3eb13533faec829583a6d3fd1f41345b62.jpg"
        var sku2 = "https://baikebcs.bdimg.com/adpic/pc%E8%AF%8D%E6%9D%A1%E9%A1%B5%E8%BD%AE%E6%92%AD%E5%8D%97%E5%A4%B4%E5%8F%A4%E5%9F%8E%E5%8D%9A%E7%89%A9%E9%A6%86270-150.png"
        var skus = mutableListOf<GoodsSku>()
        skus.add(GoodsSku(22, "SKU标题", arrayListOf(sku1, sku2)))

        for (i in 1..5) {
            val good = Goods(1, 1, "商品描述", icon, 12345,
                    "细节1介绍", "细节2较少静安寺", i,
                    20, "code-goods", "默认SKU",
                    banner, skus, 2)

            lists.add(good)
        }
        return lists
    }


}